// pages/live_application/index.js

import { isPoneAvailable } from '../../utils/reg.js'
import {postapplye,getannouncer} from '../../api/live.js'
import {getUserInfo} from '../../api/user.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		name:'',
		phone:'',
		type:'',
		success:false,
		matchid:131,
		status:-1,//0-未申请过，1-审核中，2=审核成功，3=审核失败
		isRequest:false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		console.log("matchid:"+options.matchid)
		if(options.matchid){
			this.data.matchid=options.matchid
			this._getUserInfo()
		}else{
			wx.navigateBack()
		}
	},
	_getUserInfo(){
		getannouncer(this.data.matchid).then(res=>{
			this.setData({status:res.data})
			if(res.data==1||res.data==3){//1-审核中  3=审核失败
				getUserInfo().then(res=>{
					if(res.data.announcer.name!=null){
						this.setData({name:res.data.announcer.name})
					}
					 if(res.data.announcer.phone!=null){
						this.setData({phone:res.data.announcer.phone})
					}
					 if(res.data.announcer.post!=null){
						this.setData({type:res.data.announcer.post})
					}
				})
			}else if(res.data==2){//审核成功
				wx.redirectTo({
					url: '/pages/live_center/index',
				})
			}
		})
	},
	getValue(e){
		let {value}=e.detail
		value = value.trim()

		switch(Number(e.currentTarget.dataset.index)){
			case 0:
			this.data.name = value
			break;
			case 1:
				this.data.phone = value
			break;
			default:
				this.data.type = value
			break;
		}
		let {name,phone,type}=this.data
		this.setData({success:(name!=''&&isPoneAvailable(phone)&&type!='')})
	},

	send(){
		let {isRequest,name,phone,type,success,matchid}=this.data
		if(!isRequest){
			this.data.isRequest=true
			if(!name){
				this.toast('请输入您的姓名');
			}else if(!phone){
				this.toast('请输入您的手机号码');
			}else if(!isPoneAvailable(phone)){
				this.toast('请输入正确的手机号码');
			}else if(!type){
				this.toast('请输入申请的岗位');
			}
			let succ=name!=''&&isPoneAvailable(phone)&&type!=''
			if(succ){
				postapplye({matchId:matchid,phone,post:type,name}).then(res=>{
						if(res.code==0){
							wx.showModal({
								title: '',
								content: res.msg,
								showCancel: false,
								confirmText:"好的",//默认是“确定”
								success: function (res) {
									 wx.navigateBack()
								}
						 })
						}else{
							wx.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							})
						}
						this._getUserInfo()
				})
			}
			this.data.isRequest=false
		}

	},
	toast(title,icon='none',duration=1500){
		wx.showToast({
			title: title,
			icon:icon,
			duration:duration
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})